from pydantic import BaseModel, Field
from typing import Annotated, List, Optional, Tuple, Dict
import requests,os,argparse
import re
from bs4 import BeautifulSoup
from huggingface_hub import hf_hub_download, hf_hub_url,HfApi
from huggingface_hub.constants import DEFAULT_REVISION, HUGGINGFACE_CO_URL_HOME,REPO_TYPE_MODEL
from huggingface_hub.utils import build_hf_headers,filter_repo_objects
from bypy import ByPy
from tqdm import tqdm
import hashlib
from modelscope.hub.api import HubApi

class ModelscopeUploader(BaseModel):
    repoid: str = Field(description="repo id for modelscope")
    token: str = Field(description="token for modelscope access")
    def upload(self, local: str, remote: str | None = None):
        api = HubApi()
        api.login(self.token)
        local = local or "."
        api.push_model(
            model_id=self.repoid,
            model_dir=local,
        )

def main():
    parser = argparse.ArgumentParser(
        description='同步本地模型到modelscope')
    parser.add_argument('-i', '--repoid',dest='repoid', type=str, help='id of huggingface repo')
    parser.add_argument('-p', '--path', dest='path', type=str, required=False, help='file path of local repo')
    parser.add_argument('-t', '--token', dest='token', type=str, help='token of modelscope')
    
    args,argv = parser.parse_known_args()
    uploader = ModelscopeUploader(repoid=args.repoid, token=args.token)
    uploader.upload(local=args.path)

if __name__ == '__main__':
    """
    Example:
    python sync.py -t TOKEN -i REPOID -p PATH
    同步本地模型到modelscope
    """
    main()